home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: vtf.idx.com!news
- From: Steve Mount <sjjm@hawkeye.idx.com>
- Subject: Empty String function
- Message-ID: <1996Mar14.205741.19178@vtf.idx.com>
- Sender: news@vtf.idx.com (USENET News System)
- Organization: IDX Systems Corporation
- Date: Thu, 14 Mar 1996 20:57:41 GMT
-
- I benchmarked several methods for testing a buffer's
- emptiness. Empty is defined as all characters being space,
- newline, or tab. I hit on one very fast method; it
- blew the others out of the water. I presume the standard
- function I'm using is optimized. But it has a potential
- problem I'm concerned about - can anyone tell me if it
- could be illegal, or at the least, bad form?
- Here it is:
-
- int zemp(char *ptxt, int len)
- {
- static int i;
-
- if (len == 0) len = strlen(ptxt); // Allow null-term strings
- i = strspn(ptxt," \n\t");
- if (i >= len) return(-1); // -1 == EMPTY
- return(i);
- }
-
-
- The problem is when the buffer is not null-terminated, which
- happens a lot. The strspn function COULD read past the end
- of the buffer.... potentially WAY past it. Possibly even into
- memory not owned by the program. This is where I get concerned.
- Anyone know if this could cause trouble, generally, and if
- it could cause trouble specifically in DOS/Win/Unix (it is
- used in a Unix app now).
-
- Thanks for input in advance.
-
- -Steve
-